package com.gcd.tempe.util;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * kelas adapter yang digunakan oleh listview
 * @author asus
 *
 */
public class SmartListAdapter extends BaseAdapter implements Observer {
    private LayoutInflater mInflater;
    private SmartList smartList2;
    
    class ViewHolder {
        TextView text;
    }

    public SmartListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);            		
		smartList2 = SmartList.getInstance();
		smartList2.addObserver(this);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
//            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
//            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text.setText(smartList2.get(position));
        holder.text.setBackgroundColor(position % 2 == 0 ? Color.GRAY : Color.DKGRAY);
        return holder.text;
    }

    public int getCount() {
        return smartList2.size();
    }

    public Object getItem(int position) {
        return smartList2.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

	public void update(Observable arg0, Object arg1) {
		smartList2 = SmartList.getInstance();
		notifyDataSetChanged();
		Log.v("SmartListAdapter", "Count is :"+smartList2.size());
	}
	
}